package com.heima.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.pojo.dto.VendingMachine;
import com.heima.mapper.VendingMachineMapper;
import com.heima.pojo.dto.VendingMachineQuery;
import com.heima.pojo.vo.PageVo;
import com.heima.pojo.vo.VendingMachineVo;
import com.heima.service.IVendingMachineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 存折
 * @since 2024-10-20
 */
@Slf4j
@Service
public class VendingMachineServiceImpl extends ServiceImpl<VendingMachineMapper, VendingMachine> implements IVendingMachineService {

    @Autowired
    private VendingMachineMapper vendingMachineMapper;

    @Override
    public PageVo<VendingMachineVo> query(VendingMachineQuery vendingMachineQuery) {
        String innerCode = vendingMachineQuery.getInnerCode();
        Page page = new Page<>(vendingMachineQuery.getPageIndex(), vendingMachineQuery.getPageSize());
        Page<VendingMachine> p = lambdaQuery()
                .like(innerCode != null, VendingMachine::getInnerCode, innerCode)
                .page(page);
        long pages = p.getPages();
        long total = p.getTotal();
        List<VendingMachine> records = p.getRecords();
        List<VendingMachineVo> machineVoList = new ArrayList<>();
        records.forEach(vendingMachine -> {
            VendingMachineVo vendingMachineVo = new VendingMachineVo();
            BeanUtils.copyProperties(vendingMachine,vendingMachineVo);
            Long id = vendingMachineMapper.selectNode(vendingMachine.getInnerCode());
            vendingMachineVo.setNode(vendingMachineMapper.select(id));
            log.info("接收到的id为：{}",id);
            Long regionId =  vendingMachineMapper.selectRegionId(vendingMachine.getInnerCode());
            log.info("接收到的id为：{}",regionId);
            vendingMachineVo.setRegion(vendingMachineMapper.selevtRegion(regionId));
               Long typeId= vendingMachineMapper.selectTypeId(vendingMachine.getInnerCode());
               vendingMachineVo.setType(vendingMachineMapper.selectType(typeId));
               Map<String,String> map=new HashMap<>();
          map.put("10001","true");
          map.put("10002","false.");
          map.put("10003","true");
            vendingMachineVo.setStatus(map);
            machineVoList.add(vendingMachineVo);
        });
        return new PageVo<>(vendingMachineQuery.getPageIndex(), vendingMachineQuery.getPageSize(),pages,total,machineVoList);
    }
}
